<?php

namespace App\Kuafu\Mybank\Controller;

use App\Exceptions\BaseException;
use App\Exceptions\CustomValidateException;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Mybank\Service\MybankEntryService;
use App\Kuafu\Mybank\Validate\MybankEntryValidate;
use App\Remote\Mybank\MybankCreateBatchRemote;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class MybankEntryController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function index(Request $request): JsonResponse
    {
        $pageNum         = (int)$request->get('pageNum', 1);
        $pageSize        = (int)$request->get("pageSize", 20);
        $principalMobile = $request->get("principalMobile", "");
        $status          = $request->get("status", "");
        $merchantType    = (string)$request->get("merchantType", "");
        $merchantName    = (string)$request->get("merchantName", "");
        $updateStartTime = (string)$request->get("updateStartTime", "");
        $updateEndTime   = (string)$request->get("updateEndTime", "");
        $shopInfo        = (string)$request->get("shopInfo", "");

        return $this->success(MybankEntryService::index(
            $pageNum,
            $pageSize,
            $principalMobile,
            $status,
            $merchantType,
            $merchantName,
            $updateStartTime,
            $updateEndTime,
            $shopInfo
        ));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     * @throws GuzzleException
     */
    public function create(Request $request): JsonResponse
    {
        $params = MybankEntryValidate::createValidate($request->all());
        return $this->success(MybankEntryService::create($params));
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BaseException
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $params = MybankEntryValidate::updateValidate($request->merge(["id" => $id])->all());
        MybankEntryService::update($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BaseException
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        MybankEntryValidate::detailValidate($request->merge(["id" => $id])->all());
        return $this->success(MybankEntryService::detail($id));
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BaseException
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public function remindActivate(Request $request, int $id): JsonResponse
    {
        MybankEntryValidate::remindActivateValidate($request->merge(["id" => $id])->all());
        MybankEntryService::remindActivate($id);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BaseException
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public function remindWithholding(Request $request, int $id): JsonResponse
    {
        MybankEntryValidate::remindWithholdingValidate($request->merge(["id" => $id])->all());
        MybankEntryService::remindWithholding($id);
        return $this->success();
    }

    public function freeze()
    {

    }

    public function status()
    {

    }

    /**
     *  解约申请审核确认接口
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface|BaseException
     */
    public function confirmArrangement(Request $request, int $id): JsonResponse
    {
        $prams = MybankEntryValidate::confirmArrangementValidate($request->merge(["id" => $id])->all());
        MybankEntryService::confirmArrangement($prams);
        return $this->success();
    }
}
